<?php
/**
 * 队列处理 业务逻辑
 *
 * 这里与 libary 中 队列类 的区别： 这里的queue是业务逻辑的封装
 *
 *
 * @package		Unotice v4
 * @author		Soften.cn Dev Team	| By QuWei
 * @copyright	Copyright (c) 2013, Unotice, Inc.
 * @link		http://www.soften.cn
 * @filesource
*/


class Queue_Model extends CI_Model {

	function __construct() {
		parent::__construct();

		$this->load->config('rabbitmq');
		$config = $this->config->item('rabbitmq');

		$this->load->library('QueueRabbitMq',$config);
	}

	/**
	* 入队列时的参数编码,最终返回约定过的编码格式(暂定json)
	*
	* @access public
	* @param mixed $messages 消息内容.. [Must]
	* @return string $mixed
	*/
	public function encode($messages) {
		if(empty($messages)){
			log_message('error',"Queue_model encode param is empty !!!");
			return false;
		}

		return json_encode($messages);
	}

	/**
	* 出队列时的参数解码,与encode对应
	* 通常返回值为数组..取决于 消息的格式
	*
	* @access public
	* @param mixed $messages 消息内容.. [Must]
	* @return array $mixed
	*/
	public function decode($messages) {
		if(empty($messages)){
			log_message('error',"Queue_model decode param is empty !!!");
			return false;
		}

		return json_decode($messages,true);
	}

	/**
	* 获取exchange信息
	*
	* @access public
	* @return string $mixed
	*/
	public function get_exchange() {
		return $this->config->item('exchange','rabbitmq');
	}

	/**
	* __call
	*
	* @access public
	* @param string $method_name 调用的方法名称 [Must]
	* @param array $arguments 调用此方法时传的参数 [Must]
	* @return string $mixed
	*/
     public function __call($method_name, $arguments) {
     	return call_user_func_array(array($this->queuerabbitmq, $method_name), $arguments);
     }


	/**
	 * 添加 需要抓取的站点  任务信息
	 * @param unknown_type $taskinfo
	 */
	public function add_sitetask($site_id,$queue_name) {
		if(empty($site_id)){
			log_message('error',"Queue_model add_sitetask param is empty!!");
			return false;
		}

		$exchange_name = $this->get_exchange();
		//$queue_name = 'need_crawl_site_task';
		$routing_key = $queue_name."_key" ;

		//$messages = $this->encode($taskinfo);

		return $this->queuerabbitmq->add_task($site_id, $exchange_name,$queue_name, $routing_key);
	}
	
	public function add_cloudv3($id) {
		if(empty($id)){
			log_message('error',"Queue_model add_cloudv3 param is empty!!");
			return false;
		}
	
		$exchange_name = $this->get_exchange();
		$queue_name = 'cloudv3';
		$routing_key = 'cloudv3_key';
	
		$messages = $this->encode($id);
	
		return $this->queuerabbitmq->add_task($messages, $exchange_name,$queue_name, $routing_key);
	}



	/**
	 * 加入队列更新用户文章库所有表
	 * @param $location 数组（uuid, localtion_type）
	 * @return unknown_type
	 */
	public function add_user_articlelocationtask($location) {
		if(empty($location)){
			log_message('error',"Queue_model add_user_articlelocationtask param is empty!!");
			return false;
		}

		$exchange_name = $this->get_exchange();
		$queue_name = 'need_dispatcher_article_user_task';
		$routing_key = 'need_dispatcher_article_user_task_key';

		$messages = $this->encode($location);

		return $this->queuerabbitmq->add_task($messages, $exchange_name,$queue_name, $routing_key);
	}


	/**
	 * 从队列里获取location去更新用户文章库所有表
	 */
	public function get_user_articlelocationtask() {
		$exchange_name = $this->get_exchange();
		$queue_name = 'need_dispatcher_article_user_task';
		$routing_key = 'need_dispatcher_article_user_task_key';

		$messages = $this->queuerabbitmq->get_task($exchange_name,$queue_name, $routing_key);
		return $this->decode($messages);
	}


	/**
	 * 从队列中取出一个需要抓取的站点的id
	 */
	function get_crawl_site_id($queue_name) {
		//调用 libary 中 队列类， 从队列中 返回待抓取网站
		$exchange_name = $this->get_exchange();
		//$queue_name = 'need_crawl_site_task';
		$routing_key = $queue_name."_key" ;

		$messages = $this->queuerabbitmq->get_task($exchange_name,$queue_name, $routing_key);
		//$messages_rs = $this->decode($messages);

		return $messages;
	}

	/**
	 * 添加 需要抓取的关键词  任务信息
	 * @param $taskinfo
	 */
	public function add_wordtask($id) {
		if(empty($id)){
			log_message('error',"Queue_model add_wordtask param is empty!!");
			return false;
		}

		$exchange_name = $this->get_exchange();
		$queue_name = 'need_crawl_word_task';
		$routing_key = 'need_crawl_word_task_key';

		//$messages = $this->encode($taskinfo);

		return $this->queuerabbitmq->add_task($id, $exchange_name,$queue_name, $routing_key);
	}

	/**
	 * 从队列中取出一个需要抓取的关键词的id
	 */
	function get_crawl_word_id() {
		//调用 libary 中 队列类， 从队列中 返回待抓取网站
		$exchange_name = $this->get_exchange();
		$queue_name = 'need_crawl_word_task';
		$routing_key = 'need_crawl_word_task_key';

		$messages = $this->queuerabbitmq->get_task($exchange_name,$queue_name, $routing_key);
		//$messages_rs = $this->decode($messages);

		return $messages;
	}

	function get_weixin_id() {
		$exchange_name = $this->get_exchange();
		$queue_name = 'weixin_sync_v4';
		$routing_key = 'weixin_sync_v4';

		$messages = $this->queuerabbitmq->get_task($exchange_name,$queue_name, $routing_key);
		//$messages_rs = $this->decode($messages);

		return $messages;
	}
	
	function get_paper_id() {
		$exchange_name = $this->get_exchange();
		$queue_name = 'paper';
		$routing_key = 'paper';
	
		$messages = $this->queuerabbitmq->get_task($exchange_name,$queue_name, $routing_key);
		//$messages_rs = $this->decode($messages);
	
		return $messages;
	}
	
	function get_cloudv3() {
		//调用 libary 中 队列类， 从队列中 返回待抓取网站
		$exchange_name = $this->get_exchange();
		$queue_name = 'cloudv3';
		$routing_key = 'cloudv3_key';
	
		$messages = $this->queuerabbitmq->get_task($exchange_name,$queue_name, $routing_key);
		$messages_rs = $this->decode($messages);
	
		return $messages_rs;
	}

	public function add_sync_task($uuid, $snatch_time) {
		if(empty($uuid)){
			log_message('error',"Queue_model add_wordtask param is empty!!");
			return false;
		}
	
		$exchange_name = $this->get_exchange();
		$queue_name = 'sync_task';
		$routing_key = 'sync_task_key';
	
		$taskinfo = array("uuid"=>$uuid, "snatch_time"=>$snatch_time);
		$messages = $this->encode($taskinfo);
	
		return $this->queuerabbitmq->add_task($messages, $exchange_name,$queue_name, $routing_key);
	}
	
	/*
    * 获取需要同步的文章所在库及id
   * */	
	public function get_sync_task() {
		$exchange_name = $this->get_exchange();
		$queue_name = 'sync_task';
		$routing_key = 'sync_task_key';
	
		$messages = $this->queuerabbitmq->get_task($exchange_name,$queue_name, $routing_key);
		return $this->decode($messages);
	}


	/**
     *微博 博主 队列堆积数量
     */
    public function get_v4_crawl_user_task_num(){

        $exchange_name = $this->get_exchange();

        $queue_name = 'v4_crawl_user_task';
        $routing_key = $queue_name.'_key';

        $messages = $this->queuerabbitmq->get_task_num($exchange_name, $queue_name, $routing_key);

        return $messages;
    }

    /**
     *微博 转发 队列堆积数量
     */
    public function get_zhuanfa_num(){

        $exchange_name = $this->get_exchange();

        $queue_name = 'zhuanfa';
        $routing_key = $queue_name.'_key';

        $messages = $this->queuerabbitmq->get_task_num($exchange_name, $queue_name, $routing_key);

        return $messages;
    }

    /**
     *微博 待同步数据 队列堆积数量
     */
    public function get_task_yijian_num(){

        $exchange_name = $this->get_exchange();

        $queue_name = 'task_yijian';
        $routing_key = $queue_name.'_key';

        $messages = $this->queuerabbitmq->get_task_num($exchange_name, $queue_name, $routing_key);

        return $messages;
    }

    /**
     *微博 crawl_user_info 队列堆积数量
     */
    public function get_crawl_user_info_num(){

        $exchange_name = $this->get_exchange();

        $queue_name = 'crawl_user_info';
        $routing_key = $queue_name.'_key';

        $messages = $this->queuerabbitmq->get_task_num($exchange_name, $queue_name, $routing_key);

        return $messages;
    }

}